.equ MEM_CTL_BASE,        0x48000000
.equ SDRAM_BASE,          0x30000000
.equ GPBCON,              0x56000010
.equ GPBDAT,              0x56000014

    
.text
.global _start
_start:
    bl  disable_watch_dog
    bl  init_led
    bl  turn_off_all_led
@   bl  turn_on_led1   @ use the led to debug
    bl  memsetup
@   bl  turn_on_led2
    bl  copy_stepstone_to_sdram
@   bl  turn_on_led3
    ldr pc, =on_sdram
on_sdram: 
    ldr sp, =0x34000000 @ setup stack so we can call the C routine
@   bl  turn_on_led4
    bl  main
loop:
    b loop

disable_watch_dog:
    mov r1, #0x53000000 @ set watchdog register value to zero
    mov r2, #0x0
    str r2, [r1]
    mov pc, lr

init_led:
    ldr r0, =GPBCON
    mov r1, #0x00015400 @ set GPB5-8 as output port
    str r1, [r0]
    mov pc, lr

turn_off_all_led:
    ldr r0, =GPBDAT
    mov r1, #0x000001e0
    str r1, [r0]
    mov pc, lr
    
turn_on_all_led: 
    ldr r0, =GPBDAT
    mov r1, #0x00000000
    str r1, [r0]
    mov pc, lr

turn_on_led1: 
    ldr r0, =GPBDAT
    mov r1, #0x000001d0
    str r1, [r0]
    mov pc, lr
    
turn_on_led2: 
    ldr r0, =GPBDAT
    mov r1, #0x000001b0
    str r1, [r0]
    mov pc, lr

turn_on_led3: 
    ldr r0, =GPBDAT
    mov r1, #0x00000170
    str r1, [r0]
    mov pc, lr

turn_on_led4: 
    ldr r0, =GPBDAT
    mov r1, #0x000000e0
    str r1, [r0]
    mov pc, lr
    
    
memsetup:
    mov  r1, #MEM_CTL_BASE
    adrl r2, mem_cfg_addr
    add  r3, r1, #4*13
1: 
    ldr  r4, [r2], #4
    str  r4, [r1], #4
    cmp  r1, r3
    bne  1b
    mov  pc, lr

copy_stepstone_to_sdram:
    mov  r1, #0
    ldr  r2, =SDRAM_BASE
    mov  r3, #4*1024
1: 
    ldr  r4, [r1], #4
    str  r4, [r2], #4
    cmp  r1, r3
    bne  1b
    mov  pc, lr

.align 4
mem_cfg_addr:
    .long   0x22011110      @ BWSCON
    .long   0x00000700      @ BANKCON0
    .long   0x00000700      @ BANKCON1
    .long   0x00000700      @ BANKCON2
    .long   0x00000700      @ BANKCON3
    .long   0x00000700      @ BANKCON4
    .long   0x00000700      @ BANKCON5
    .long   0x00018005      @ BANKCON6
    .long   0x00018005      @ BANKCON7
    .long   0x008C07A3      @ REFRESH
    .long   0x000000B1      @ BANKSIZE
    .long   0x00000030      @ MRSRB6
    .long   0x00000030      @ MRSRB7
    
